package com.example.weblogaspect.metod2.interceptor;

import com.example.weblogaspect.constant.OperationLogHeaderConstant;
import lombok.extern.slf4j.Slf4j;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.aspectj.lang.JoinPoint;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;

/**
 * 过滤器
 */
@Slf4j
public class LogAdvice implements MethodInterceptor {
    ThreadLocal<Long> startTime = new ThreadLocal<>();

    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        //===========================================
        //这里做你的before操作
        doBefore(invocation);
        //===========================================

        Object result = invocation.proceed();
        System.out.println(result);

        //===========================================
        //这里做你的after操作
        doAfterReturning(invocation, result);

        return result;
    }

    /**
     * 执行前
     *
     * @param invocation
     * @throws Throwable
     */
    public void doBefore(MethodInvocation invocation) throws Throwable {
        startTime.set(System.currentTimeMillis());
        // 接收到请求，记录请求内容
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attributes != null) {
            attributes.setAttribute(OperationLogHeaderConstant.START_TIME, startTime.get(), 1);
            if (attributes.getRequest() != null) {
                HttpServletRequest request = attributes.getRequest();
                String info = String.format("Method:{%s} Host:{%s} Path:{%s} Query:{%s}",
                        request.getMethod(),
                        request.getScheme(),
                        request.getRequestURL().toString(),
                        request.getParameterMap());
                log.info("gateway filter=>{}", info);
                //log.info("gateway filter.Query=>{}",);
                // 记录下请求内容
                log.info("URL : {}", request.getRequestURL().toString());
                log.info("HTTP_METHOD : {}", request.getMethod());
                log.info("IP : {}", request.getRemoteAddr());
                //log.info("CLASS_METHOD : {}", joinPoint.getSignature().getDeclaringTypeName() + ":" + joinPoint.getSignature().getName());
                //log.info("ARGS : {}", joinPoint.getArgs());
            }
        }
        System.out.println("Before method");
        Object[] arguments = invocation.getArguments();
        System.out.println("getArguments:" + arguments);
        System.out.println(arguments);
        System.out.println(arguments.length);
        System.out.println("getMethod:" + invocation.getMethod());
        Method method = invocation.getMethod();
        System.out.println("getMethod:" + method.getName());
        System.out.println("getDeclaringClass:" + method.getDeclaringClass());
        System.out.println("getParameterTypes:" + method.getParameterTypes());
        System.out.println("getStaticPart:" + invocation.getStaticPart());
        System.out.println("getThis:" + invocation.getThis());
    }

    /**
     * after操作
     *
     * @param ret
     * @throws Throwable
     */
    public void doAfterReturning(MethodInvocation invocation, Object ret) throws Throwable {
        System.out.println("After method");
    }
}
